//
//  Globals.m
//  Conference
//
//  Created by chenxin on 12-11-15.
//
//

#import "Globals.h"

@implementation Globals

@synthesize sLanguage;
@synthesize sAlarmUpdate;
@synthesize sNewsUpdate;
@synthesize sPickUpUpdate;
@synthesize sSeeOffUpdate;
@synthesize sPlaceUpdate;
@synthesize sEleSpeakUpdate;
@synthesize progress_view;
@synthesize servierUrl;
@synthesize currentHeight;
@synthesize isThreadExit;
static Globals *sharedManager=nil;
-(id)init{
	if (self =[super init]) {
        sLanguage=@"false";
         sAlarmUpdate=@"";
         sNewsUpdate=@"";
         sPickUpUpdate=@"";
         sSeeOffUpdate=@"";
         sPlaceUpdate=@"";
         sEleSpeakUpdate=@"";
        servierUrl=@"http://app.citssh.com.cn/";
        currentHeight=0;
         UIView *view=[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
		 view.backgroundColor=[[UIColor grayColor] colorWithAlphaComponent:0.0f];
        
        UIImageView *image_bg=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_load.png"]];
        image_bg.frame=CGRectMake(115, 140, 90, 90);
        
        [view addSubview:image_bg];
        [image_bg release];
		
		UIActivityIndicatorView *indicatorView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
		[indicatorView startAnimating];
        
		indicatorView.frame= CGRectMake(142,167 , 37, 37);
        
		[view addSubview:indicatorView];
		[indicatorView release];
		self.progress_view=view;
		[view release];
        
    }
    return self;
}

+(Globals *)getInstance{
	@synchronized(self){
		if(sharedManager ==nil){
			sharedManager=[[self alloc] init];
		}
	}
	return sharedManager;
}


-(void)showAlertView:(NSString *) msg{
    
    UIAlertView *alertView =[[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    
    [alertView show];
    
    [alertView release];
    
}
-(CGSize)CalculationCellSize:(CGFloat)contentWidth fontSize:(int)fontSize content:(NSString*)content{
// 列寬
//CGFloat contentWidth = self.table.frame.size.width;
// 字体
UIFont *font = [UIFont systemFontOfSize:fontSize];
//NSString *content=data.message;
// 計算出顯示完內容需要的最小尺寸
CGSize size = [content sizeWithFont:font constrainedToSize:CGSizeMake(contentWidth, 1000) lineBreakMode:UILineBreakModeWordWrap];
    return size;
}
//缩放
- (UIImage *)scaleImage:(UIImage *)image
{
    float scaleSize=1.0;
    //    int h=image.size.height;
    int w=image.size.width;
    if(w>520){
        float x=w/(520.0);
        scaleSize=1/x;
    }
    
    UIGraphicsBeginImageContext(CGSizeMake(image.size.width*scaleSize/2,image.size.height*scaleSize/2));
    [image drawInRect:CGRectMake(0,0, image.size.width * scaleSize/2, image.size.height *scaleSize/2)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}


-(NSMutableArray*)getImageArray:(NSArray *)img{
    NSMutableArray *arrayOfImg=[NSMutableArray arrayWithCapacity:0];
    for(int i=0;i<[img count];i++){
        NSString *sImg=[img objectAtIndex:i];
        if (nil == sImg || [@"" isEqualToString:sImg])
            continue;
        NSString *Url=[NSString stringWithFormat:@"%@%@",self.servierUrl,sImg];
        NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[Url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
        UIImage *image=[self scaleImage:[UIImage imageWithData:data]];
        if(image==nil)
            image=[UIImage imageNamed:@"attachment_default_pic.jpg"];
        UIImageView *imageView=[[UIImageView alloc]initWithImage:image];
        
        [imageView setTag:i];
        imageView.contentMode = UIViewContentModeCenter;
        imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight );
        [arrayOfImg addObject:imageView];
        
        [imageView release];
    }
    return arrayOfImg;
}

-(NSMutableArray *)addDefaultImage:(NSArray *)url scroll:(UIScrollView *)scrollView height:(int)height{
    UIImage *image=[UIImage imageNamed:@"attachment_default_pic.jpg"];
    NSMutableArray *arrayOfUrl=[NSMutableArray arrayWithCapacity:0];
    for (int i=0;i<[url count]; i++) {
        NSString *sImg=[url objectAtIndex:i];
        if (nil == sImg || [@"" isEqualToString:sImg])
            continue;
        [arrayOfUrl addObject:sImg];
        UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, height, 80, 80)];
        imageView.contentMode = UIViewContentModeCenter;
        [imageView setTag:i];
        [imageView setImage:image];
        imageView.center=CGPointMake(140,height+50);
        height=height+100;
        [scrollView addSubview:imageView];
        [imageView release];
    }
    [scrollView setContentSize:CGSizeMake(scrollView.frame.size.width, height)];
    return arrayOfUrl;
}

-(void)dealloc{

    [progress_view release];
    [super dealloc];
}
@end
